package com.mc.lp.database.dao.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.mc.lp.database.dao.ISocialBehaviourRecordDAO;
import com.mc.lp.database.entity.SocialBehaviorRecordEntity;
import com.mc.lp.database.entity.type.RecordType;
import com.mc.lp.database.service.handler.util.GenericAggregator;
/*
 * @author aphanda
 * @date Sep 21, 2014
 * @time 1:17:18 AM
 */
@Repository
public class SocialBehaviorRecordDAOImpl extends
AbstractGenericHibernateDAO<SocialBehaviorRecordEntity, Long, ISocialBehaviourRecordDAO> implements
ISocialBehaviourRecordDAO{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4732847598202094903L;

	@Override
	public void saveAcademics(SocialBehaviorRecordEntity e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<SocialBehaviorRecordEntity> fetchDetailsByEntityIdTermGrade(Long entityId, int gradeId,
			int termId) {
		List<SocialBehaviorRecordEntity> acadList = new ArrayList<SocialBehaviorRecordEntity>();
		Criteria crit = this.getCurrentSession().createCriteria(SocialBehaviorRecordEntity.class);
		crit.add(Restrictions.eq("termId", termId));
		crit.add(Restrictions.eq("gradeId", gradeId));
		crit.add(Restrictions.eq("studentEntity",entityId));
		acadList = crit.list();
		return acadList;

	}
	
	@Override
	public List<SocialBehaviorRecordEntity> fetchSocialBehavioursByGrades(Long studentEntity, List<Integer> gradeValueList) throws DataAccessException {
		List<SocialBehaviorRecordEntity> socioBehavList = new ArrayList<SocialBehaviorRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(SocialBehaviorRecordEntity.class);
		crit.add(Restrictions.in("gradeId", gradeValueList));
		crit.addOrder(Order.desc("gradeId"));
		crit.addOrder(Order.desc("termId"));
		crit.add(Restrictions.eq("studentEntity",studentEntity));
		socioBehavList = crit.list();
		return socioBehavList;
	}

	
	
	@Override
	public List<GenericAggregator> getStrengthForSocialBehaviour(Long studentEntityId, List<Integer> gradeValueList) {
		
		List<GenericAggregator> resultList = new ArrayList<GenericAggregator>();
		String stringStrengthAndWeakNessQuery = "select GRADE, SOCIAL_BEHAVIOUR_ID, AVG(ATTRIBUTE_WEIGHTAGE) as weigtage "
				+ "from SOCIAL_BEHAVIOR_RECORD where  STUDENT_ENTITY_ID = ? and GRADE in (:gradeValueList) "
				+ "group by SOCIAL_BEHAVIOUR_ID, GRADE "
				+ "order by GRADE;";
		
		SQLQuery query = getCurrentSession().createSQLQuery(stringStrengthAndWeakNessQuery);
		
		query.setBigInteger(0, BigInteger.valueOf(studentEntityId));
		query.setParameterList("gradeValueList", gradeValueList);
		query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

		List data = query.list();
		for (Object object : data) {
			GenericAggregator grade = new GenericAggregator();
			//grade.set
			Map row = (Map) object;
			try{
				grade.setGradeId(Integer.parseInt(row.get("GRADE").toString()));
				grade.setKeyIdentifier(((BigInteger)row.get("SOCIAL_BEHAVIOUR_ID")).longValue());
				grade.setWeightage(((BigDecimal)row.get("weigtage")).intValue());
				grade.setRecordTypeId(RecordType.SOCIAL_BHEAVIOUR.getId());
				resultList.add(grade);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return resultList;
	}
	
}

